package com.lanou3g.code0418.wait;

/**
 * Created by dllo on 18/4/18.
 */
/*
 wait() sleep() 区别 : 都会让线程停止
    区别 1 :wait ()是object 类的方法 任何对象都有
    sleep是 Thread类的方法,
    区别2: wait() 必须写在synchronized 代码块中, 并且 必须是 锁对象调用
    wait()方法,而sleep没有限制(不需要synchronized)
    区别3: wait()会释放锁对象,而sleep()不会释放锁
    区别4:wait()可以被别的线程唤醒,而 sleep()需要等待时间结束
    */

public class Main {
    public static void main(String[] args) {
        final Object lock = new Object();
        Thread a = new Thread() {
            @Override
            public void run() {
                System.out.println("a线程开始了");
                synchronized (lock) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("a线程结束了");
            }
        };
        a.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("准备唤醒a线程");
        synchronized (lock) {
            lock.notify();

        }
    }
}
